﻿using System;
using System.Collections.Generic;
using System.Linq;
using SARS.Biz.Facades;
using SARS.DAO;
using SARS.DAO.Facades;
using SARS.Entity;

namespace SARS.Biz
{
    public class SessionBO : ISessionBO
    {
        private readonly ICourseDAO _courseDao = new CourseDAO();
        private readonly ISessionDAO _dao = new SessionDAO();
        private readonly IRoomDAO _roomDao = new RoomDAO();
        private readonly IUserDAO _userDao = new UserDAO();

        public List<SessionEntity> RetrieveAllSession()
        {
            return _dao.RetrieveAllSession();
        }

        public SessionEntity RetrieveSessionById(int sessionId)
        {
            SessionEntity session = _dao.RetrieveSessionById(sessionId);
            session.Manager = _userDao.RetrieveUserById(session.ManagerID);
            session.Room = _roomDao.RetrieveRoomById(session.RoomID);
            return session;
        }

        public int InsertSession(SessionEntity entity)
        {
            List<StudentCourseEntity> studentCourseList = _courseDao.RetrieveStudentCourseByCourseId(entity.CourseID);

            int sessionId = 0;

            if (studentCourseList.Count > 0)
            {
                sessionId = _dao.InsertSession(entity);

                if (sessionId > 0)
                {
                    List<AttendanceSessionEntity> attendanceSessionList =
                        studentCourseList.Select(studentCourse => new AttendanceSessionEntity
                        {
                            SessionID = sessionId,
                            StudentID = studentCourse.StudentID,
                            IsAbsent = 0,
                            Creator = 1,
                            CreatedDate = DateTime.Now
                        }).ToList();

                    _dao.InsertAttendanceSession(attendanceSessionList);
                }
            }

            return sessionId;
        }

        public List<SessionEntity> RetrieveSessionByCourseId(int courseId)
        {
            return _dao.RetrieveSessionByCourseId(courseId);
        }

        public int InsertAttendanceSession(List<AttendanceSessionEntity> entities)
        {
            return _dao.InsertAttendanceSession(entities);
        }

        public List<AttendanceSessionEntity> RetrieveAttendanceSessionBySessionId(int sessionId)
        {
            return _dao.RetrieveAttendanceSessionBySessionId(sessionId);
        }

        public int UpdateSessionById(SessionEntity entity)
        {
            return _dao.UpdateSessionById(entity);
        }

        public int DeleteSessionById(int id)
        {
            int result = _dao.DeleteSessionById(id);

            if (result > 0)
                DeleteAttendanceSessionById(id);

            return result;
        }

        public int UpdateAttendanceSessionById(AttendanceSessionEntity entity)
        {
            return _dao.UpdateAttendanceSessionById(entity);
        }

        public int DeleteAttendanceSessionById(int id)
        {
            return _dao.DeleteAttendanceSessionById(id);
        }

        public List<AttendanceSessionEntity> RetrieveAttendanceSessionAll()
        {
            return _dao.RetrieveAttendanceSessionAll();
        }

        public List<AttendanceSessionEntity> GetAttendanceSessionByCourseIdAndStudentId(int courseId, int studentId)
        {
            List<AttendanceSessionEntity> listAttendance = _dao.GetAttendanceSessionByCourseIdAndStudentId(courseId,
                studentId);
            foreach (AttendanceSessionEntity att in listAttendance)
            {
                att.Session = _dao.GetSessionById(att.SessionID);
                att.Session.Manager = _userDao.RetrieveUserById(att.Session.ManagerID);
                att.Session.Room = _roomDao.RetrieveRoomById(att.Session.RoomID);
            }

            return listAttendance;
        }

        public List<SessionEntity> GetSessionsByManagerId(int managerId)
        {
            return _dao.GetSessionsByManagerId(managerId);
        }

        public SessionEntity GetSessionById(int sessionId)
        {
            SessionEntity session = _dao.GetSessionById(sessionId);
            session.Manager = _userDao.RetrieveUserById(session.ManagerID);
            session.Room = _roomDao.RetrieveRoomById(session.RoomID);
            return session;
        }
    }
}